﻿Imports CRM.BusinessLayer
Imports System.Data
Partial Public Class frmAdminInactiveAccountManagers
    Private _Frm_AccountType As Integer
    Public Property Frm_AccountType() As Integer
        Get
            Return _Frm_AccountType
        End Get
        Set(ByVal value As Integer)
            _Frm_AccountType = value
        End Set
    End Property
    Private Sub Btn_Close_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Close.Click
        Me.Close()
    End Sub

    Public Sub SQL_InactiveAM()
        Try
            Dim StrSQL As String
            StrSQL = "Select Con_ID, Con_Name as 'Name' from L_Consultant "
            StrSQL = StrSQL & " Where Con_Status = 0 and Con_ID in ("
            If Me.Frm_AccountType = 1 Then
                StrSQL = StrSQL & "Select distinct Person_AccountManager from D_Person)"
            Else
                StrSQL = StrSQL & "Select distinct Company_AccountManagerID from D_Company)"
            End If
            Dim listAccountManagers As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
            Me.List_Inactive.ItemsSource = listAccountManagers
            Me.List_Inactive.Columns(0).IsVisible = False
            Me.List_Inactive.Columns(1).Width = New GridLength(225)
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminInactiveAccountManagers-SQL_InactiveAM")
        End Try
    End Sub

    Private Sub frmAdminInactiveAccountManagers_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        Me.Frm_AccountType = 1
        Me.Toggle7.IsChecked = True
        SQL_InactiveAM()
    End Sub

    Private Sub Toggle7_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Toggle7.Checked
        Me.Frm_AccountType = 1
        SQL_InactiveAM()
    End Sub

    Private Sub Toggle8_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Toggle8.Checked
        Me.Frm_AccountType = 2
        SQL_InactiveAM()
    End Sub

    Private Sub Btn_Reassign_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Reassign.Click
        Try
            Dim selectedAccountManager As DataRow = Me.List_Inactive.SelectedItem
            If selectedAccountManager Is Nothing Then
                Call MsgBox("Please select a consultant first", , "Error")
                Exit Sub
            End If

            Dim StrSQL As String, StrMess As String, StrMsg As String

            StrMsg = "Account Manager changed from " & selectedAccountManager(1) & " to unassigned with the admin tool, as that user is now inactive"

            StrSQL = "Declare @Error integer " & vbCrLf & vbCrLf
            StrSQL = StrSQL & "Begin Tran InactiveAM" & vbCrLf & vbCrLf

            StrSQL = StrSQL & "Insert into D_Event "
            StrSQL = StrSQL & "(Event_Description, Event_Category, Event_Message, Event_Enteredby, Event_LinkType, Event_LinkID)"
            StrSQL = StrSQL & " Select 'Details Changed',0,'" & StrMsg & "'," & GlobalCRM.CurrentConID & ", "


            If Me.Frm_AccountType = 1 Then

                StrSQL = StrSQL & "2, Person_ID "
                StrSQL = StrSQL & " From D_person where Person_AccountManager = " & selectedAccountManager(0)

                StrSQL = StrSQL & "Select @Error = @@Error"
                StrSQL = StrSQL & " If @Error != 0"
                StrSQL = StrSQL & " begin"
                StrSQL = StrSQL & " rollback tran InactiveAM"
                StrSQL = StrSQL & " Return"
                StrSQL = StrSQL & " End"


                StrSQL = StrSQL & vbCrLf & vbCrLf

                StrSQL = StrSQL & "Update D_Person set Person_AccountManager = 11 "
                StrSQL = StrSQL & " Where Person_AccountManager = " & selectedAccountManager(0)

                StrMess = "All Private accounts with " & selectedAccountManager(1) & " as account manager, set to unassigned"
            Else
                StrSQL = StrSQL & "3, Company_ID"
                StrSQL = StrSQL & " From D_Company where Company_AccountManagerID = " & selectedAccountManager(0)

                StrSQL = StrSQL & vbCrLf & vbCrLf

                StrSQL = StrSQL & "Select @Error = @@Error"
                StrSQL = StrSQL & " If @Error != 0"
                StrSQL = StrSQL & " begin"
                StrSQL = StrSQL & " rollback tran InactiveAM"
                StrSQL = StrSQL & " Return"
                StrSQL = StrSQL & " End"

                StrSQL = StrSQL & vbCrLf & vbCrLf

                StrSQL = StrSQL & "Update D_Company set Company_AccountManagerID = 11 "
                StrSQL = StrSQL & " Where Company_AccountManagerID = " & selectedAccountManager(0)

                StrMess = "All Corporate accounts with " & selectedAccountManager(1) & " as account manager, set to unassigned"
            End If

            StrSQL = StrSQL & vbCrLf & vbCrLf

            StrSQL = StrSQL & "Select @Error = @@Error"
            StrSQL = StrSQL & " If @Error != 0"
            StrSQL = StrSQL & " begin"
            StrSQL = StrSQL & " rollback tran InactiveAM"
            StrSQL = StrSQL & " Return"
            StrSQL = StrSQL & " End"

            StrSQL = StrSQL & vbCrLf & vbCrLf & "Commit Tran InactiveAM"

            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then Exit Sub

            Call (New TasksEvents).GblFunc_Add_SysEvent("Admin (InactiveAM)", StrMess)

            Me.SQL_InactiveAM()
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminInactiveAccountManagers-Btn_Reassign_Click")
        End Try
    End Sub
End Class
